package com.yanqu.road.server.cmd.activity.relation;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.RelationActivityModule;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@CmdApi(reqMsg = CookBoyActivityProto.CookBoyActivityGetRewardReqMsg.class, respMsg = CookBoyActivityProto.CookBoyActivityGetRewardRespMsg.class)
@Cmd(code = Protocol.S_COOK_BOY_ACTIVITY_GET_REWARD_ONE_KEY, desc = "一键领取活动奖励(第三阶段（场次奖励）)")
public class RelationActivityOneKeyGetRewardCmd extends AbstractCommand {

    private int activityId;

    private int conditionId;

    private String params;

    private ActivityRewardResult result;

    private ActivityInfo activityInfo;

    private boolean isServer = true;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CookBoyActivityProto.CookBoyActivityGetRewardReqMsg reqMsg = CookBoyActivityProto.CookBoyActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        conditionId = reqMsg.getConditionId();
        activityId = reqMsg.getActivityId();
        params = reqMsg.getParams();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityConditionInfo conditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(conditionInfo != null){
            activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
            if(!ActivityHelper.activityInShowTime(activityInfo) || (activityInfo.getType() != eActivityType.CookingBoyActivity.getValue() && activityInfo.getType() != eActivityType.ChefDuel.getValue())){
                ret = GameErrorCode.E_ERROR_PARAMETER;
                return;
            }
            if(activityInfo != null){
                if(conditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralUserCrossRank.getValue() ||
                        conditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralServerCrossRank.getValue() ||
                        conditionInfo.getType() == eGamePlayerEventType.CookBoy1IntegralUnionCrossRank.getValue() ||
                        conditionInfo.getType() == eGamePlayerEventType.CookBoy2IntegralUserCrossRank.getValue() ||
                        conditionInfo.getType() == eGamePlayerEventType.CookBoy3IntegralUserCrossRank.getValue()){
                    result = player.getModule(RelationActivityModule.class).sendCrossRankReward(activityId,conditionId, conditionInfo.getType());
                    isServer = false;
                }else if(conditionInfo.getType() == eGamePlayerEventType.CookBoy3OpenBanquetTimes.getValue() ||
                        conditionInfo.getType() == eGamePlayerEventType.CookBoy3AllBanquetUserCount.getValue() ||
                        conditionInfo.getType() == eGamePlayerEventType.ChefDuel3AllBanquetUserCount.getValue()){
                    result = player.getModule(RelationActivityModule.class).sendCrossSpecialReward(activityId,conditionId, conditionInfo.getType(),params,true);
                    isServer = false;
                }else{
                    result = player.getModule(RelationActivityModule.class).getActivityRewardOneKey(activityId, conditionId);
                }
                ret = result.getRet();
            }
        }else{
            ret = GameErrorCode.E_ORE_ACTIVITY_GET_REWARD_TYPE_ERROR;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(isServer || ret != 0){
            CookBoyActivityProto.CookBoyActivityGetRewardRespMsg.Builder respMsg = CookBoyActivityProto.CookBoyActivityGetRewardRespMsg.newBuilder();
            respMsg.setRet(ret);
            if(ret == 0){
                respMsg.setReward(result.getReward());
            }
            player.sendPacket(Protocol.U_COOK_BOY_ACTIVITY_GET_REWARD_ONE_KEY, respMsg);
        }
    }
}
